<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="backupNow" style="margin-left: 16px;">立即备份</el-button>
    </div>

    <el-divider />

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading" stripe
      highlight-current-row>
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column label="备份文件名" prop="filename" />
      <el-table-column label="备份最新修改时间" prop="lastModifyTime" />
      <el-table-column label="文件大小" prop="fileSize" />
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button plain type="warning" size="mini" @click="deleteIt(scope.row.filename)" title="删除该备份文件">删除</el-button>
            <el-button plain type="danger" size="mini" @click="restoreIt(scope.row.filename)" title="立即将数据库还原到该文件导出的时候">还原</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" title="详情">
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, backup, restore, clean, deleteBackUp } from '@/api/database'

export default {
  components: {
    Pagination
  },
  data() {
    return {
      dialogVisible: false,
      list: [],
      listLoading: true,
      listQuery: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData()
    },
    refresh() {
      this.listQuery = {
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        if(response.code === 200) {
          this.list = response.data
          this.listLoading = false
        } else {
          this.$message.warning("获取列表失败：" + response.msg);
          this.listLoading = false
        }
      })
    },
    // 删除指定的备份文件
    deleteIt(filename) {
      this.$confirm('确认删除该数据库备份文件吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          filename: filename
        }
        deleteBackUp(params).then(response => {
          if (response.code === 200) {
            this.$message.success("删除成功");
            this.fetchData()
          } else {
            this.$message.error("删除失败：" + response.msg);
          }
        })
      })
    },
    // 还原到指定的备份文件
    restoreIt(filename) {
      this.$confirm('一旦还原，将丢失从该备份文件创建到目前为止的所有，请确认要将数据库恢复到该备份文件吗？', '重要提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        let params = {
          filename: filename
        }
        restore(params).then(response => {
          if(response.code === 200) {
            this.$message.success("还原成功！");
            this.listLoading = false
          } else {
            this.$message.error("还原失败：" + response.msg);
            this.listLoading = false
          }
        })
      })
    },
    // 立即备份
    backupNow() {
      this.listLoading = true
      backup().then(response => {
        if(response.code === 200) {
          this.$message.success("数据库备份成功！");
          this.fetchData()
        } else {
          this.$message.error("数据库备份失败：" + response.msg);
          this.listLoading = false
        }
      })
    }
  }
}
</script>
